var schoolTreeSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    callback: {
        onClick: chooseSchool
    }
};
var schoolTree;

/*function update(item) {
    //vm.getInfo(item)
    vm.showList = false;
    vm.title = "修改";
    //var id = item;
    if(item == null){
        return ;
    }

        var url = "school/profession/update"

        $.ajax({
            type: "POST",
            url: baseURL + url,
            contentType: "application/json",
            data: JSON.stringify(vm.profession),
            success: function(r){
                if(r.code === 0){
                    alert('操作成功', function(index){
                        vm.reload();
                        //   location. reload();
                    });
                }else{
                    alert(r.msg);
                }
            }
        });
}*/
function del(item) {
    var ids = item;
    if (ids == null) {
        return;
    }

    confirm('确定要删除选中的记录？', function () {
        $.ajax({
            type: "GET",
            url: baseURL + "school/profession/delete?id=" + ids,
            success: function (r) {
                if (r.code == 0) {
                    alert('操作成功');
                    $("#jqGrid").trigger("reloadGrid");

                } else {
                    alert(r.msg);
                }

            }
        });
    });
}

function delYear(item) {
    var ids = item;
    if (ids == null) {
        return;
    }

    confirm('确定要删除选中的记录？', function () {
        $.ajax({
            type: "GET",
            url: baseURL + "school/profession/deleteYearScore?id=" + ids,
            success: function (r) {
                if (r.code == 0) {
                    alert('操作成功');
                    $("#jqGridProfessional").trigger("reloadGrid");

                } else {
                    alert(r.msg);
                }

            }
        });
    });
}


function chooseSchool(event, treeId, treeNode) {
    vm.school.id = treeNode.id
    vm.school.name = treeNode.name
    vm.schoolName = treeNode.name
    vm.profession = {id: null, code: null, name: null, type: '普通', wenlike: ""}
    vm.reload()
}

$(function () {
    vm.getSchoolTree()
    $("#jqGrid").jqGrid({
        url: baseURL + 'school/profession/list',
        datatype: "json",
        colModel: [
            {label: '专业代码', name: 'code', width: 40},
            {label: '专业名称', name: 'name', width: 120},
            {label: '文理科', name: 'wenlike', width: 40},
            {label: '专业类型', name: 'type', width: 40},
            {label: '专业级别', name: 'level', width: 40},
            {
                label: '操作', name: 'id', width: 40,
                formatter: function (data) {
                    return '<span class="label label-warning" onclick="del(' + data + ')">删除</span>';

                }
            },
            {
                label: '操作', name: 'id', width: 40,
                formatter: function (data) {
                    return '<span class="label label-warning" data-toggle="modal" data-target="#myModal">修改</span>';

                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        },
        onCellSelect: function (id, code, name, b, a) {
            $.ajax({
                type: "GET",
                url: baseURL + "school/profession/info/" + id,
                success: function (r) {
                    if (r.code == 0) {
                        vm.profession = r.profession
                        vm.reloadProfessional()
                        console.log(r)

                    } else {
                        alert(r.msg);
                    }

                }
            });


        }
    });
    $("#jqGridProfessional").jqGrid({
        url: baseURL + `school/profession/getProfessionLowestScore`,
        datatype: "json",
        colModel: [
            // { label: '学校id', name: 'schoolId', index: 'school_id', width: 80, hidden:true },
            {label: '年份', name: 'year', index: 'year', width: 80},
            {label: '专业名称', name: 'schoolCode', index: 'school_code', width: 80},
            {label: '专业代码', name: 'schoolName', index: 'school_name', width: 80},
            {label: '专业性质', name: 'schoolName', index: 'school_name', width: 80},
            {label: '文科最低分', name: 'wenkeScore', index: 'putongwenke', width: 80},
            {label: '理科最低分', name: 'likeScore', index: 'putonglike', width: 80},
            {
                label: '操作', name: 'id', width: 80,
                formatter: function (data) {
                    return '<span class="label label-warning" onclick="delYear(' + data + ')">删除</span>';

                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: false,
        // pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
})


$(function () {
    new AjaxUpload('#mallProfession', {
        action: baseURL + "school/profession/saveMall",
        name: 'file',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, extension) {
            if (!(extension && /^(xls|xlsx)$/.test(extension.toLowerCase()))) {
                alert('只支持excel！');
                return false;
            }
        },
        onComplete: function (file, r) {
            if (r.code == 100) {
                $("#jqGridProfessional").trigger("reloadGrid");
                alert("导入成功")
                vm.reload()
            } else {
                alert(r.msg);
            }
        },
    });
});


var vm = new Vue({
    el: '#rrapp',
    data: {
        school: {id: null, name: null},
        profession: {id: null, code: null, name: null, type: '普通', wenlike: "", level: ""},
        professionLowScore: {year: null},
        schoolName: null,
        types: ["普通", "校企合作", "中外合作", "艺术", "春考", "3+2连读"],
        wenlike: ["文科", "理科", "文理兼收"],
        showList: true,
        showYearList: true,
        title: null,
        noHasSchoolYear: ["2018", "2017", "2016"],
        lowscore: {},
        level: ["本科", "专科"],
        editPage: false,
    },
    methods: {
        getInfoUpdata: function () {
            // var id = item
            // $.ajax({
            //     type: "GET",
            //     url: baseURL + "school/profession/info?"+id,
            //     contentType: "application/json",
            //     //data: JSON.stringify(vm.basicscore),
            //     success: function (r) {
            //         if (r.code == 0) {
            //             vm.profession = r.data
            //
            //         } else {
            //             // alert(r.msg);
            //         }
            //     }
            // });
        },
        addPYearScore: function () {
            if (vm.profession.id == null) {
                alert("请选择专业")
            } else {
                vm.showYearList = false
            }
        },
        courseTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = zCoursetree.getSelectedNodes();
                    if (node[0].id < 10000) {
                        //选择上级菜单
                        vm.teachPlan.courseId = node[0].id;
                        vm.teachPlan.title = node[0].name;
                        layer.close(index);
                    }
                }
            });
        },
        getInfoUpdata: function () {
            console.log(vm.profession)
            $.ajax({
                type: "POST",
                url: baseURL + "school/profession/update",
                contentType: "application/json",
                data: JSON.stringify(vm.profession),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        getSchoolTree: function (menuId) {
            //加载学校树
            $.get(baseURL + "school/school/getSchoolTree", function (r) {
                schoolTree = $.fn.zTree.init($("#schoolTree"), schoolTreeSetting, r.schoolList);
                var node = schoolTree.getNodeByParam("id", 0);
                schoolTree.selectNode(node);
            })
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'schoolId': vm.school.id},
                page: page
            }).trigger("reloadGrid");
        },
        reloadProfessional: function (event) {
            vm.showList = true;
            var page = $("#jqGridProfessional").jqGrid('getGridParam', 'page');
            $("#jqGridProfessional").jqGrid('setGridParam', {
                postData: {
                    'schoolId': vm.school.id,
                    'professionId': vm.profession.id
                },
                page: page
            }).trigger("reloadGrid");
        },
        add: function () {
            if (vm.school.id == null) {
                alert("请先选择学校")
            } else {
                vm.showList = false;
                vm.title = "新增";
                vm.lowscore = {year: null, schoolId: vm.school.id}
            }
        },
        saveOrUpdate: function () {
            var url = "school/profession/save";
            vm.profession.schoolId = vm.school.id

            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.profession),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                            //   location. reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        saveYear: function () {
            vm.showYearList = true
            vm.professionLowScore.schoolId = vm.school.id
            vm.professionLowScore.schoolName = vm.school.name
            vm.professionLowScore.professionId = vm.profession.id
            vm.professionLowScore.professionName = vm.profession.name
            vm.professionLowScore.professionCode = vm.profession.code
            vm.professionLowScore.professionType = vm.profession.type
            if (vm.professionLowScore.year == null) {
                alert("年份有误")

            } else {
                var url = "school/profession/saveProfessionYear";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.professionLowScore),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reloadProfessional();
                                //   location. reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }


        },
        reloadYear: function () {
            vm.showYearList = true
        }

    }

});